
function MaterialBuilder(){
	
	var _repMateriales = {};
	
	//<<CONSTANTES>> estas constantes definidas en el archivo de configuracion
		
	this.alreadyGenerated = function(tipo, nivel){
		if (_repMateriales[tipo+nivel]) return true;
		else return false;		
	};
	
	var _meshMat = null;
	this.getMeshMaterial = function(){
		if (_meshMat) return _meshMat;
		
		_meshMat = new THREE.MeshFaceMaterial();
		return _meshMat;
	};
	
	this.setMaterial = function(tipo, mat){
		_repMateriales[tipo] = mat;		
	};
	
	//console.log("***********************_repMateriales",_repMateriales); TODO
	
	var cc = 0;
	//tipo : tipo del material a crear
	//nivel : nivel al que pertenece el objeto que har� uso del material obtenido
	//params.nuevo : crea el material sin buscarlo en el repositorio
	//params.<algo> : par�metros espec�ficos para un de material en particular
	
	//necesito pasarle el nivel, porque genero los materiales por nivel, para poder cambiar la opacidad
	//de un nivel entero (esto es asi desde que se mergean los spaces)
	this.getMaterial = function(tipo, nivel, params){
		
		
		if (!tipo) {alert("tipo de material invalido: "+tipo); return null;}
		
		
		var _nuevo = (params == undefined || params.nuevo == undefined)? false : params.nuevo;
		
		var _generatedMaterial = null;
		
		//si el material ya esta creado y no se quiere uno 'nuevo', retornamos el del repositorio
		if (_repMateriales[tipo+nivel] && !_nuevo) {
			return _repMateriales[tipo+nivel];
		};
		
		var transparente = false;
		
		switch(tipo){
		
				case "linea_camino":
							_generatedMaterial = new THREE.LineBasicMaterial( { linewidth: CONSTANTS.ANCHO_LINEA_ANIMADA, color: CONSTANTS.COLOR_LINEA_ANIMADA,	opacity: 1 });
					break;
				
				case "pared" : 	
								//alert('CREANDO MATERIAL DEPRECATED. no deberia haber entrado');
								_generatedMaterial = new THREE.MeshLambertMaterial( { color:  CONSTANTS.COLOR_PAREDES, transparent: transparente,} );		
					break;
				case "techo" : 	
					_generatedMaterial = new THREE.MeshLambertMaterial( { color: 0x777777, transparent: transparente,} );		
					break;
				
					
					/////////////////////
					
				case "tr" : 
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_CORRIDOR_COLOR, transparent: transparente, /*jeib 9/11 //blending: 4//THREE.AdditiveAlphaBlending*/} );	
					break;
				case "of" : 
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_OFFICE_COLOR, transparent: transparente, /*jeib 9/11 //blending: 4//THREE.AdditiveAlphaBlending*/} );	
					break;
				case "ot" : 
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_OTHER_COLOR, transparent: transparente,} );
					break;
				case "au" :
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_CLASSROOM_COLOR, transparent: transparente,} );
					break;
				case "pi" :
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_FLOOR_COLOR, transparent: transparente,} );
					break;
					
				case "portal_interno" : 
//					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: 0xa08560, transparent: transparente} );
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: 0x0000ff, transparent: transparente} );
//					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_CORRIDOR_COLOR, transparent: transparente} );
					break;
				case "portal_externo" : //break;
					//_generatedMaterial =  new THREE.MeshLambertMaterial( { color: CONSTANTS.SPACE_CORRIDOR_COLOR, transparent: transparente} );
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: 0xff0000, transparent: transparente} );
					break;
				case "portal_externo_dangling" : //break;
					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: 0x00ff00, transparent: transparente} );
					//					_generatedMaterial =  new THREE.MeshLambertMaterial( { color: 0x7a5e38, transparent: transparente} );
					break;
					
					
					////////////////////////
					
					
				case "linea_space" :
								alert('CREANDO MATERIAL DEPRECATED. no deberia haber entrado');
								_generatedMaterial = new THREE.LineBasicMaterial( { color: CONSTANTS.COLOR_LINEAS_SPACE, linewidth: CONSTANTS.ANCHO_LINEAS_SPACE } );
					break;
					
				case "centroide" :
								alert('CREANDO MATERIAL DEPRECATED. no deberia haber entrado');
								_generatedMaterial = new THREE.MeshBasicMaterial( { color: CONSTANTS.COLOR_CENTROIDE, wireframe: true } );
					break;
					
				case "landmark" :				
					break;
					
				case "escalon":
								_generatedMaterial = new THREE.MeshLambertMaterial( { color: CONSTANTS.COLOR_ESCALON , shading: THREE.FlatShading, vertexColors: THREE.VertexColors } );
					break;
					
				case "texto" :  
					
					if (nivel != undefined) _generatedMaterial =  new THREE.MeshPhongMaterial( { color: CONSTANTS.COLOR_TEXTO_3D, wireframe: false } );	
					else  _generatedMaterial =  new THREE.MeshPhongMaterial( { color: CONSTANTS.COLOR_NOMBRE_CALLES, wireframe: false } );	
					break;
					
				case "arrow_portal_interno" :
								alert('CREANDO MATERIAL DEPRECATED. no deberia haber entrado');
					 			_generatedMaterial =  new THREE.MeshBasicMaterial({color: CONSTANTS.COLOR_PORTALES_INTERNOS});	
					break;
					
				case "arrow_portal_externo" :
								alert('CREANDO MATERIAL DEPRECATED. no deberia haber entrado');
								_generatedMaterial =  new THREE.MeshBasicMaterial({color: CONSTANTS.COLOR_PORTALES_EXTERNOS});	
					break;
					
				default : _generatedMaterial =  new THREE.MeshLambertMaterial( { color: 0xff0000, transparent: transparente,} );
				//default : alert("tipo de material invalido: "+tipo); return null;
		
		}
		
		if (!_repMateriales[tipo+nivel]) {_repMateriales[tipo+nivel] = _generatedMaterial;}
		return _generatedMaterial;
		
		
	};
	
}
MaterialBuilder.MeshFaceMaterial =  new THREE.MeshFaceMaterial();